package com.example.wan_android.ui.fragment

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.wan_android.BR
import com.example.wan_android.R
import com.example.wan_android.adapter.NavAdapter
import com.example.wan_android.adapter.SystemAdapter
import com.example.wan_android.databinding.FragmentSystemBinding
import com.example.wan_android.tools.SpacesItemDecoration
import com.example.wan_android.viewmodel.SystemViewModel

class SystemFragment : BaseFragment<FragmentSystemBinding, SystemViewModel>(
    SystemViewModel::class.java,
    R.layout.fragment_system,
    BR.viewModel
) {

    private val systemAdapter by lazy { SystemAdapter(mViewModel!!) }
    private val navAdapter by lazy {
        NavAdapter(mViewModel!!)
    }

    override fun initViews() {

        systemAdapter.context = context
        navAdapter.context = context
        trueSystem()
        mViewDataBinding?.rbSystem?.setOnClickListener {
            trueSystem()
        }
        mViewDataBinding?.rbNavigation?.setOnClickListener {
            trueNav()
        }

        mViewModel?.isSuccess?.observe(this) {
            if (it) {
                mViewDataBinding?.ivEmpty?.visibility = View.GONE
                mViewDataBinding?.rvSystem?.visibility = View.VISIBLE
            } else {
                mViewDataBinding?.ivEmpty?.visibility = View.VISIBLE
                mViewDataBinding?.rvSystem?.visibility = View.GONE
            }
        }
    }

    fun trueSystem() {
        mViewModel?.getSystems()
        mViewDataBinding?.rvSystem?.adapter = systemAdapter
        mViewDataBinding?.rvSystem?.layoutManager = LinearLayoutManager(context)
        mViewDataBinding?.rvSystem?.addItemDecoration(SpacesItemDecoration(20))
        mViewModel?.systemItemLiveData?.observe(this) {
            it.onSuccess {
                systemAdapter.setListData(it)
            }
        }
    }

    fun trueNav() {
        mViewModel?.getNavItems()
        mViewDataBinding?.rvSystem?.adapter = navAdapter
        mViewDataBinding?.rvSystem?.layoutManager = LinearLayoutManager(context)
        mViewDataBinding?.rvSystem?.addItemDecoration(SpacesItemDecoration(20))
        mViewModel?.navLiveData?.observe(this) {
            it.onSuccess {
                navAdapter.setListData(it)
            }
        }
    }
}